package com.leo.springbootmultithreadbasic.lock.reentrantlock.basicdemo.demo2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Leo
 */
public class MySerivce {
    private Logger logger = LoggerFactory.getLogger(MySerivce.class);

    private Lock lock = new ReentrantLock();

    public void methodA() {
        try {
            lock.lock();

            logger.info(String.format("methodA begin ThreadName=%s  time=%s", Thread.currentThread().getName(), System.currentTimeMillis()));
            logger.info(String.format("methodA end ThreadName=%s  time=%s", Thread.currentThread().getName(), System.currentTimeMillis()));

            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void methodB() {
        try {
            lock.lock();

            logger.info(String.format("methodA begin ThreadName=%s   time=%s", Thread.currentThread().getName(), System.currentTimeMillis()));
            logger.info(String.format("methodA end ThreadName=%s   time=%s", Thread.currentThread().getName(), System.currentTimeMillis()));

            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
